// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.jasmin.unit.math;

import com.dustedpixels.jasmin.unit.Unit;

/**
 * Implementation of SUB32 combined from ADD32 and NOT32s.
 * 
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class SUB32Combo implements Unit {

  // (input) input 1, for the left operand
  public int IN_LEFT;
  
  // (input) input 2, for the right operand
  public int IN_RIGHT;
  
  // (input) carry bit
  public int IN_CARRY;
  
  // (output) output
  public int OUT;

  // Composite units.
  private final ADD32 add32;
  private final NOT32 not32;
  private final NOT32 not32carry;

  public SUB32Combo() {
    this.add32 = new ADD32();
    this.not32 = new NOT32();
    this.not32carry = new NOT32();
  }
  
  public void updateEndPoints() {
    not32.IN = IN_RIGHT;
    not32.updateEndPoints();
    
    not32carry.IN = IN_CARRY;
    not32carry.updateEndPoints();
    
    add32.IN_LEFT = IN_LEFT;
    add32.IN_RIGHT = not32.OUT;
    add32.IN_CARRY = not32carry.OUT;
    add32.updateEndPoints();
    
    OUT = add32.OUT;
  }
  
  // For testing purposes only.
  public static void main(String[] args) {
    SUB32Combo sub32 = new SUB32Combo();
    
    sub32.IN_LEFT = 10;
    sub32.IN_RIGHT = 12;
    sub32.IN_CARRY = 0;
    sub32.updateEndPoints();
    System.out.println("10 - 12 = " + sub32.OUT);
    
    sub32.IN_LEFT = 10;
    sub32.IN_RIGHT = 12;
    sub32.IN_CARRY = 1;
    sub32.updateEndPoints();
    System.out.println("10 - CARRY - 12 = " + sub32.OUT);    
  }
}
